(function(window, undefined) {
	var dino = window.dino = window.dino || {};


	// 工厂
	// 创建精灵
	dino.create = function(name, opts) {
		var dic = {
			"grass": function() {
				var info = dino.infoDic["grass"];
				var obj = new backgroundObj({
					name: info.name,
					size: info.size,
					filename: info.textureUrl,
					rect: cc.RectMake(0, 0, info.size.width, info.size.height)
				});
				return obj;
			},
			"tree": function() {
				var info = dino.infoDic["tree"];
				var obj = new staticObj({
					name: info.name,
					size: info.size,
					filename: info.textureUrl,
					rect: cc.RectMake(0, 0, info.size.width, info.size.height)
				});


				obj.attach('afterStatusChanged', function(opts) {
					if(opts.nstat == 'create') {
						this.changeSkin(opts.nstat);
						this.changeStatus("normal");
					} else if(opts.nstat == 'normal') {
						// var animation = dino.getSkin(this.name, 'normal');
						// var action = cc.Animate.create(animation);
						// var rpt = cc.RepeatForever.create(action);
						// this.runAction(rpt);
					}
				}.bind(obj));



				return obj;
			},
			"chris": function() {
				var info = dino.infoDic["chris"];
				var obj = new moveObj({
					name: info.name,
					size: info.size,
					filename: info.textureUrl,
					rect: cc.RectMake(0, 0, info.size.width, info.size.height),
					speed: 100,
					canControl: true,
					handle: {
						"afterStatusChanged": function(opts) {
							if(opts.nstat == "create") {
								this.changeSkin(opts.nstat);
								this.changeStatus("normal");
							} else if(opts.nstat == "normal") {
								this.changeSkin([opts.nstat, ".", obj.direction].join(""));
							}
						},
						"afterDireChanged": function(opts) {
							this.changeSkin(this.status + "." + opts.currDire);

						},
						"beforeMove": function(opts) {
							// 检测是不是可以移动
							var checks = {};
							checks.outOfMap = function(opts) {
								return true;
							};
							var flag = true;
							for(var i in checks) {
								var ci = checks[i];
								if(!ci(opts)) {
									var flag = false;
									break;
								}
							}
							if(flag) {
								this.setPosition(opts.nextPosi);
							}
							return true;
						}
					}
				});

				window.chris=obj;

				return obj;
			}
		};
		if(dic[name]) {
			return dic[name](opts);
		} else {
			throw "create sprite failed!!";
		}
	}


	// 获取动画皮肤
	dino.getSkin = function(name, skinName) {
		var ac = cc.AnimationCache.getInstance();
		var infoDic = dino.infoDic;

		var key = [name, skinName].join('.');
		var rst;
		while(!(rst = ac.getAnimation(key))) {
			// 图片地址
			var ak = infoDic[name]['animDic'];
			var imgUrl = ak[skinName].textureUrl || infoDic[name].textureUrl; // 如果没有专门的skin贴图,则使用默认的贴图
			var steps = ak[skinName].steps;
			var size = ak.size || infoDic[name].size;
			if(!imgUrl) {
				break;
			}
			// 贴图
			var tc = cc.TextureCache.getInstance();
			var texture = tc.addImage(imgUrl);
			// 帧
			var fc = cc.SpriteFrameCache.getInstance();
			var spriteFrames = [];
			for(var i = 0; i < steps.length; i++) {
				var si = steps[i];
				var frame = cc.SpriteFrame.createWithTexture(texture, new cc.Rect(size.width * si[1], size.height * si[0], size.width, size.height));
				spriteFrames.push(frame);
			}
			// 帧间隔
			var delay = dino.config.animationDelay;
			// 动画
			var anim = cc.Animation.create(spriteFrames, delay);
			// 加入动画缓存
			ac.addAnimation(anim, key);
		}
		return rst;
	};



	// config
	dino.config = {
		// 动画间隔
		animationDelay: .2
	};

	// 按键记录
	dino.keys = {};

	// 默认值
	dino.DEFAULT = {
		keyMap: {
			"87": function(key, opts) {
				this.changeDire("up");
				this.move(opts.dt);
			},
			"65": function(key, opts) {
				this.changeDire("left");
				this.move(opts.dt);
			},
			"83": function(key, opts) {
				this.changeDire("down");
				this.move(opts.dt);
			},
			"68": function(key, opts) {
				this.changeDire("right");
				this.move(opts.dt);
			},
		},
		speed: 0,
		direction: 'down'
	};


})(window);